﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Mirkuz.Mapping
{
    public class WayPoint : MapPoint
    {
        public string Description="";
        public string Name="";
        
        public WayPoint(XmlReader r)
        {
            StreamFromKML(r);
        }

        public WayPoint(string name, string description, double latitude, double longitude, int altitude) 
            : base(latitude,longitude,altitude)
        {
            Name = name;
            Description = description;            
        }

        public void StreamToKML(XmlWriter xtr)
        {
            xtr.WriteStartElement("Placemark");
            {
                xtr.WriteStartElement("name");
                xtr.WriteString(Name);
                xtr.WriteEndElement();

                xtr.WriteStartElement("description");
                xtr.WriteString(Description);
                xtr.WriteEndElement();

                xtr.WriteStartElement("Point");
                {
                    xtr.WriteStartElement("coordinates");
                    xtr.WriteString(
                        Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + ","
                        + Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + ","
                        + Altitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
                    xtr.WriteEndElement();
                }
                xtr.WriteEndElement();
            }
            xtr.WriteEndElement();
        }

        public void StreamFromKML(XmlReader xrd)
        {
            while (xrd.Read())
            {
                if (xrd.IsStartElement())
                {
                    if (xrd.LocalName.ToLower() == "multigeometry")
                        throw new Exception("Not a waypoint");
                    if (xrd.LocalName.ToLower() == "linestring")
                        throw new Exception("Not a waypoint"); 

                    if (xrd.LocalName.ToLower()=="name")                   
                        Name = xrd.ReadElementContentAsString();

                    if (xrd.LocalName.ToLower() == "description")
                        Description=xrd.ReadElementContentAsString();
                    
                    if (xrd.LocalName.ToLower() == "coordinates")
                    {                            
                        string coords = xrd.ReadElementContentAsString();
                        string[] c = coords.Split(',');
                        Longitude = double.Parse(c[0], System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
                        Latitude = double.Parse(c[1], System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
                        Altitude = (int)double.Parse(c[2], System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
                    }                        
                }     
            }
        }
    }
}
